package com.example.lovewordsapplication.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.lovewordsapplication.MyApplication;
import com.example.lovewordsapplication.R;

/**
 * @author Leung
 * @date 2020/4/7 23:37
 */
public class SettingsFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {

    private MyApplication app;

    public SettingsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_settings, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        app = MyApplication.getInstance();
        // 显示版本名称
        TextView tvVersion = view.findViewById(R.id.tv_version);
        tvVersion.setText(app.getVersion());
        // 网络开关
        Switch btnNetworkSwitch = view.findViewById(R.id.btn_networkSwitch);
        btnNetworkSwitch.setOnCheckedChangeListener(this);
        boolean isNetWork = app.isNetWork();
        btnNetworkSwitch.setChecked(isNetWork);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        app.setNetWork(isChecked);
    }
}
